home *** CD-ROM | disk | FTP | other *** search
/ X User Tools / X User Tools (O'Reilly and Associates)(1994).ISO / sources / xbmbrows / xbmbro31.z / xbmbro31 / xbmbrowser3.1 / misc.c < prev    next >
C/C++ Source or Header  |  1993-08-05  |  5KB  |  183 lines

  1. /*
  2. *****************************************************************************
  3. ** xbmbrowser version 3.1  (c) Copyright Ashley Roll, 1992.
  4. ** FILE: misc.c
  5. **
  6. ** xbmbrowser is Public Domain. However it, and all the code still belong to me.
  7. ** I do, however grant permission for you to freely copy and distribute it on 
  8. ** the condition that this and all other copyright notices remain unchanged in 
  9. ** all distributions.
  10. **
  11. ** This software comes with NO warranty whatsoever. I therefore take no
  12. ** responsibility for any damages, losses or problems that the program may 
  13. ** cause.
  14. *****************************************************************************
  15. */
  16.  
  17. #include "xbmbrowser.h"
  18. #include "user-menu.h"
  19. #include <pwd.h>
  20.  
  21.  
  22. /*
  23. ** these two procedures handle the help window 
  24. **
  25. ** the first is the callback for the 'Done' button the other created the 
  26. ** window and puts the stuff in it.
  27. */
  28. Widget h_shell;
  29.  
  30. void Quit_Help(w,client_data,call_data )
  31. Widget w;
  32. XtPointer client_data,call_data;
  33. {
  34.   XtPopdown(h_shell);
  35. }
  36.  
  37. void Show_Help(w,client_data,call_data )
  38. Widget w;
  39. XtPointer client_data,call_data;
  40. {
  41. Widget button,about,form;
  42. static int created=0;
  43.  
  44.    if(created) {
  45.      XtPopup(h_shell,XtGrabNone);
  46.    } else {
  47.      /* create the popup shell to put the about into */
  48.      h_shell = XtVaCreatePopupShell("Help_Window",shellWidgetClass,bfw,NULL);
  49.  
  50.      form = XtVaCreateManagedWidget("Help_fw",formWidgetClass,h_shell,NULL);
  51.  
  52.     /* create the widget to put the about into */
  53.     about = XtVaCreateManagedWidget("text",asciiTextWidgetClass,form,
  54.             XtNdisplayCaret,(XtArgVal)False,
  55.             XtNscrollVertical,(XtArgVal)XawtextScrollWhenNeeded,
  56.             XtNscrollHorizontal,(XtArgVal)XawtextScrollWhenNeeded,
  57.             XtNwidth,(XtArgVal)575,
  58.             XtNheight,(XtArgVal)200,
  59.             XtNtype,(XtArgVal)XawAsciiFile,
  60.             XtNstring,(XtArgVal)HELPFILE,NULL);
  61.  
  62.  
  63.      button =  XtVaCreateManagedWidget(
  64.                "Done",commandWidgetClass,form,XtNlabel,(XtArgVal)"Done",
  65.                XtNfromVert,(XtArgVal)about,NULL);
  66.      XtAddCallback(button,"callback",Quit_Help,NULL);
  67.  
  68.      created = 1;
  69.  
  70.      XtPopup(h_shell,XtGrabNone);
  71.      XStoreName(XtDisplay(h_shell),XtWindow(h_shell),"xbmbrowser Help");
  72.      XSetIconName(XtDisplay(h_shell),XtWindow(h_shell),"xbmbrowser Help");
  73.    }
  74. }
  75.  
  76. /*------------------------------------------------------------------------*/
  77.  
  78. /* 
  79. ** sort a linked list 
  80. */
  81. static Item *r;  /* pointer to the first element in the list 2 b sorted */
  82.  
  83. Item *merge(a,b)
  84.   Item *a, *b;
  85. {
  86.   Item  aux, *temp = &aux;
  87.  
  88.   while(b != NULL)
  89.     if(a == NULL) { 
  90.       a = b; 
  91.       break;
  92.     } else
  93.     if( strcmp( b->fname, a->fname ) > 0) {
  94.       temp = temp->next = a; 
  95.       a = a->next;
  96.     } else {
  97.       temp = temp->next = b;
  98.       b = b->next;
  99.     }
  100.  
  101.   temp->next = a;
  102.   return(aux.next);
  103. }
  104.  
  105. Item *sort(n)
  106.   int n;
  107. {
  108.   Item *fi,*la, *temp;
  109.  
  110.   if(r == NULL) return(NULL);
  111.   else if(n > 1)
  112.     return(merge(sort(n/2),sort((n+1)/2)));
  113.   else {
  114.     fi = r;
  115.     la = r;
  116.     /* build list as long as possible */
  117.     for(r = r->next; r != NULL;)
  118.       if(strcmp(r->fname,la->fname) >= 0) {
  119.         la->next = r;
  120.         la = r;
  121.         r = r->next;
  122.       }
  123.       else if(strcmp(r->fname,fi->fname) <= 0) {
  124.         temp = r;
  125.         r = r->next;
  126.         temp->next = fi;
  127.         fi = temp;
  128.       }
  129.       else break;
  130.     
  131.     la->next = NULL;
  132.     return(fi);
  133.   }
  134. }
  135.  
  136. /*------------------------------------------------------------------------*/
  137.  
  138. Item *get_files(dir)
  139. /* gets all the file names from the directory 'dir' and then put
  140. ** them into a linked list.
  141. */
  142.   char *dir;
  143. {
  144.   DIR            *dirp;
  145.   struct dirent  *dp;
  146.   Item           *list=NULL, *item;
  147.   int             count;
  148.   struct stat     buf;
  149.  
  150.   dirp = opendir(dir);
  151.   for( count = 1;  ( dp = readdir(dirp) ) != NULL;   count++) {
  152.  
  153.     if(list == NULL) {
  154.       list = alloc_item();
  155.       item = list;
  156.     } else {
  157.       item->next = alloc_item();
  158.       item = item->next;
  159.     }
  160.  
  161.     strncpy(item->fname, dp->d_name, MAXNAMLEN+1);
  162.  
  163.     stat(item->fname, &buf); /* discover information about this file */
  164.  
  165.     if( S_ISDIR(buf.st_mode) )     /* check directory */
  166.       item->type = Dir;
  167.     else
  168.     if( ! S_ISREG(buf.st_mode) )   /* ie: a device, pipe, but not a link */
  169.       item->type = Bad;            /* this type should be deleted from list */
  170.     else {
  171.       /* a regular file or link to such a file */
  172.       item->type = File;
  173.       item->mtime = buf.st_mtime;
  174.     }
  175.   }
  176.  
  177.   closedir(dirp);
  178.  
  179.   r = list;
  180.   return  sort(count);
  181. }
  182.  
  183.